package com.shuzhi.stationarysource.modules.job.task;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shuzhi.stationarysource.modules.job.domain.*;
import com.shuzhi.stationarysource.modules.job.repository.BusinessDataRepository;
import com.shuzhi.stationarysource.modules.job.repository.WryBaseInfoRepository;
import com.shuzhi.stationarysource.modules.job.repository.WryEnvBaseInfoRepository;
import com.shuzhi.stationarysource.modules.job.repository.WryKeyInfoRepository;
import com.shuzhi.stationarysource.modules.job.service.FixedSourceService;
import com.shuzhi.stationarysource.modules.job.service.WryBaseInfoService;
import com.shuzhi.stationarysource.modules.job.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName TestTask
 * @Description TestTask
 * @Author 孙峰
 * @Date 2020/3/10 10:13
 * @Version 1.0
 */
@Slf4j
@Component
public class TestTask {

    private FixedSourceService fixedSourceService;

    private WryBaseInfoService wryBaseInfoService;

    private WryBaseInfoRepository wryBaseInfoRepository;

    private WryKeyInfoRepository wryKeyInfoRepository;

    private BusinessDataRepository businessDataRepository;



    public TestTask(FixedSourceService fixedSourceService, WryBaseInfoService wryBaseInfoService, WryBaseInfoRepository wryBaseInfoRepository, WryKeyInfoRepository wryKeyInfoRepository, WryEnvBaseInfoRepository wryEnvBaseInfoRepository, BusinessDataRepository businessDataRepository) {
        this.fixedSourceService = fixedSourceService;
        this.wryBaseInfoService = wryBaseInfoService;
        this.wryBaseInfoRepository = wryBaseInfoRepository;
        this.wryKeyInfoRepository = wryKeyInfoRepository;
        this.businessDataRepository = businessDataRepository;
    }

    /**
     * 推送污染源数据，获取固定点源数据并保存  跟新污染源keyInfo 中的固定点源代码和wryBaseInfo中的是否为省部级推送数据
     * @param addFixSource
     * @return 返回操作数据id
     */
    private AddFixSource handleData(AddFixSource addFixSource) {

        String query="ENTERID="+addFixSource.getENTERID()+
                //污染源名称
                "&ENTERNAME=思路固定源"+addFixSource.getENTERNAME()+
                "&ENTERADDRESS="+addFixSource.getENTERADDRESS()+
                "&TYSHXYDM="+addFixSource.getTYSHXYDM()+
                //行政区名称
                "&REGIONNAME="+addFixSource.getREGIONNAME()+
                "&CODE_TRADE="+addFixSource.getCODE_TRADE()+
                "&TRADENAME="+addFixSource.getTRADENAME()+
                "&LONGITUDE="+addFixSource.getLONGITUDE().substring(0,10)+
                "&LATITUDE="+addFixSource.getLATITUDE().substring(0,9)+
                "&STATUS="+ addFixSource.getSTATUS()
//              // 行政区编码 "+addFixSource.getCODE_REGION()+"
                +"&CODE_REGION=370000000000"
                +"&DATASOURCE=9b7202888fdf1de45d415387aae233e9";
        log.info(query);
        try {
            String s = HttpUtils.GetResponse(query);
            JSONObject jsonObject = JSONObject.parseObject(s);
            if("100".equals( jsonObject.get("messageCode"))){
                log.info("成功解析");

                JSONArray jsonArray = JSONObject.parseArray(jsonObject.get("data").toString());
                String data = jsonArray.get(0).toString();

                FixedSource fixedSource = JSONObject.parseObject(data, FixedSource.class);
                log.info(fixedSource.toString()+"");
               // BusinessData businessData = JSONObject.parseObject(data, BusinessData.class);
                //保存工商数据
               // businessDataRepository.save(businessData);
                //保存固定点源数据
                fixedSourceService.save(fixedSource);
                //修改污染源是否为固定污染源状态
              //  wryBaseInfoRepository.modifyingIsbstsStatus(Long.parseLong(addFixSource.getENTERID()));
                //添加污染源固定编码
             //   wryKeyInfoRepository.modifyingPollutionCd(fixedSource.getEntercode(),addFixSource.getENTERID());
            }

        } catch (IOException e) {
            log.info(e.toString());
            e.printStackTrace();
        }

        return addFixSource;
    }
    public void run(){
        log.info("开始执行将污染源主库未增加到固定点源的数据新增任务");
        while (true){

            PageRequest of = PageRequest.of(0, 1000);
            // 获取所有污染源主库未增加到固定点源的数据
            List<AddFixSource> allNoPush = wryBaseInfoService.findAllNoPush(of);
            log.info(allNoPush.size()+"");
            if(allNoPush.isEmpty()){
                log.info("数据处理完毕");
                break;
            }
            log.info("开始处理数据");
            allNoPush.parallelStream().forEach(
                    this::handleData
            );


        }
        log.info("执行完毕");
    }

    public void run1(String str){
        log.info("执行成功，参数为： {}" + str);
        AddFixSource byId = wryBaseInfoService.findById(9028L);
        log.info("获取到的数据为"+byId);
        handleData(byId);
        log.info("执行完毕");

    }
}
